/* eslint-disable */
import Vue from 'vue'
import Router from 'vue-router'
import baseRouter from "./baseRouter.js" //末级路由（完全自定义)
Vue.use(Router)

var link = () => import('@/views/layout/link') // 路由索引
var layout = () => import('@/views/layout/layout') // 主页框架
var frame = () => import('@/views/layout/frame') // 菜单框架

/* 根据末级路由，克隆高级路由，主要用来区分name属性 */
function routerConvert(routers, prefix) {
	var routerList = [];
	if (Array.isArray(routers)) {
		routerList = JSON.parse(JSON.stringify(routers));
		for (let i = 0, len = routers.length; i < len; i++) {
			if (prefix && routerList[i].name) {
				routerList[i].name = prefix + "/" + routerList[i].name;
			}
			for (var k in routers[i]) {
				if (typeof routers[i][k] == "function") {
					routerList[i][k] = routers[i][k]
				}
			}
		}
	}
	return routerList;
}
/* 自动添加多级路由 
layout/frame/${path} //顶部导航+侧边导航
layout/${path} //顶部导航
frame/${path} //侧边导航
roleBindFun //独立页面
*/
function multiRouters(rList) {
	return [{
		path: '/',
		component: link,
		children: [{
				path: 'layout',
				component: layout,
				children: [{
					path: 'frame',
					component: frame,
					children: routerConvert(rList, "layout/frame")
				}]
			}, //一级路由（自动添加顶部导航和侧边栏）
			{
				path: 'layout',
				component: layout,
				children: routerConvert(rList, "layout")
			}, //二级路由（自动添加顶部导航）
			{
				path: 'frame',
				component: frame,
				children: routerConvert(rList, "frame")
			}, //二级路由（自动添加侧边栏）
			...rList //末级路由（完全自定义）
		]
	}]
}
/* routerMatch方法为自动匹配路由框架
根据跳转前页面的layout和frame，智能控制动态路由跳转的框架
避免出现重复的路由嵌套
*/
function routerMatch(routerParams) {
	let pathname = location.pathname; //获取主页面路径
	if (routerParams.name) { //路由name参数改造
		if (pathname.indexOf("frame/") == -1) {
			routerParams.name = routerParams.name.replace("frame/", "")
		} else if (routerParams.name.indexOf("frame/") == -1) {
			routerParams.name = "frame/" + routerParams.name;
		}
		if (pathname.indexOf("layout/") == -1) {
			routerParams.name = routerParams.name.replace("layout/", "")
		} else if (routerParams.name.indexOf("layout/") == -1) {
			routerParams.name = "layout/" + routerParams.name;
		}
	}
	if (routerParams.path) { //路由path参数改造
		if (pathname.indexOf("/frame/") == -1) {
			routerParams.path = routerParams.path.replace("/frame/", "/")
		} else if (routerParams.path.indexOf("/frame/") == -1) {
			routerParams.path = "/frame/" + routerParams.path;
		}
		if (pathname.indexOf("/layout/") == -1) {
			routerParams.path = routerParams.path.replace("/layout/", "/")
		} else if (routerParams.path.indexOf("/layout/") == -1) {
			routerParams.path = "/layout/" + routerParams.path;
		}
		routerParams.path = routerParams.path.replace("//", "/")
	}
	return routerParams;
}
Vue.prototype.pageLink = link; //主页布局插件存储在vue原型链上，方便动态添加子路由
Vue.prototype.pageLayout = layout; //主页布局插件存储在vue原型链上，方便动态添加子路由
Vue.prototype.pageFrame = frame; //主页布局插件存储在vue原型链上，方便动态添加子路由
Vue.prototype.multiRouters = multiRouters; //动态添加多级路由的方法
Vue.prototype.routerMatch = routerMatch; //自定匹配路由嵌套层级
/* 二级路由（自动添加侧边栏） */
var frameRouter = {
	path: 'frame',
	component: frame,
	children: routerConvert(baseRouter, "frame")
}
/* 二级路由（自动添加顶部导航） */
var layoutRouter = {
	path: 'layout',
	component: layout,
	children: routerConvert(baseRouter, "layout")
};
/* 一级路由（自动添加顶部导航和侧边栏） */
var layoutFrameRouter = {
	path: 'layout',
	component: layout,
	children: [{
		path: 'frame',
		component: frame,
		children: routerConvert(baseRouter, "layout/frame")
	}]
};
/* 摄影比赛（自定义应用页面框架）*/
var photograph = {
	path: 'photograph',
	component: () => import('@/views/photograph/photographLayout'),
	// redirect: '/works?appId=7c5abb3a4eb145aca09af4a6253b0804',
	children: [{
			path: 'works',
			component: () => import('@/views/photograph/works'),
			meta: {
				metaInfo: {
					title: "摄影作品展"
				}
			}
		},
		{
			path: 'myVotes',
			component: () => import('@/views/photograph/myVotes'),
			meta: {
				metaInfo: {
					title: "我的投票"
				}
			}
		}
	]
}
/* 路由配置 */
const routerOptions = {
	mode: 'history',
	scrollBehavior: () => ({
		y: 0
	}),
	routes: [{
			path: '/',
			component: link,
			redirect: '/layout/homepage' //测试环境默认首页
		},
		{
			path: '/login', //登录页
			component: () => import('@/views/login/myLogin')
		},
		{
			path: '/',
			component: link,
			children: [
				layoutFrameRouter, //一级路由（自动添加顶部导航和侧边栏）
				layoutRouter, //二级路由（自动添加顶部导航）
				frameRouter, //二级路由（自动添加侧边栏）
				...baseRouter, //末级路由（完全自定义）
				photograph //摄影投票
			]
		},
		{
			path: '/preview', // 预览
			component: () => import('@/views/synergy/portalManage/preview')
		}
	]
};


const router = new Router(routerOptions);

export default router;
